package com.xzx.study;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetworkActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = NetworkActivity.class.getSimpleName();

    private Button network_get1;
    private Button network_get2;
    private Button network_get3;
    private Button network_post1;
    private Button network_post2;
    private Button network_post3;
    private TextView network_response;
    private EditText network_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network);

        network_url = findViewById(R.id.network_url);
        network_response = findViewById(R.id.network_response);
        network_get1 = findViewById(R.id.network_get1);
        network_get2 = findViewById(R.id.network_get2);
        network_get3 = findViewById(R.id.network_get3);
        network_post1 = findViewById(R.id.network_post1);
        network_post2 = findViewById(R.id.network_post2);
        network_post3 = findViewById(R.id.network_post3);

        network_get1.setOnClickListener(this);
        network_get2.setOnClickListener(this);
        network_get3.setOnClickListener(this);
        network_post1.setOnClickListener(this);
        network_post2.setOnClickListener(this);
        network_post3.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        if (v == network_get1) {
            handleGet1();
        } else if (v == network_post1) {
            handlePost1();
        } else if (v == network_get2) {
            handleGet2();
        }
    }

    private void handleGet2() {
    }

    private void handlePost1() {
        final ProgressDialog dialog = ProgressDialog.show(this, null, "加载中。。。。");
        new Thread() {
            @Override
            public void run() {
                try {
                    URL url = new URL(network_url.getText().toString());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    connection.connect();

                    OutputStream outputStream = connection.getOutputStream();
                    outputStream.write("{\"name\":\"xzx\"}".getBytes());
                    outputStream.flush();
                    final StringBuilder builder = new StringBuilder();
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200) {
                        InputStream inputStream = connection.getInputStream();
                        byte[] buffer = new byte[1024 * 1000];
                        int len;
                        while ((len = inputStream.read(buffer)) > 0) {
                            builder.append(new String(buffer, 0, len));
                        }
                        inputStream.close();
                    }
                    outputStream.close();
                    connection.disconnect();
                    runOnUiThread(() -> network_response.setText(builder.toString()));
                } catch (Exception e) {
                    Log.e(TAG, "网络请求异常", e);
                }
                dialog.dismiss();
            }
        }.start();
    }

    private void handleGet1() {
        final ProgressDialog dialog = ProgressDialog.show(this, null, "加载中。。。。");
        new Thread() {
            @Override
            public void run() {
                try {
                    URL url = new URL(network_url.getText().toString());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    connection.connect();

                    final StringBuilder builder = new StringBuilder();
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200) {
                        InputStream inputStream = connection.getInputStream();
                        byte[] buffer = new byte[1024 * 1000];
                        int len;
                        while ((len = inputStream.read(buffer)) > 0) {
                            builder.append(new String(buffer, 0, len));
                        }
                        inputStream.close();
                    }
                    connection.disconnect();
                    runOnUiThread(() -> network_response.setText(builder.toString()));
                } catch (Exception e) {
                    Log.e(TAG, "网络请求异常", e);
                }
                dialog.dismiss();
            }
        }.start();
    }
}
